package com.platform.controller;

import java.util.List;
import java.util.Map;

import com.platform.entity.MerchantInfomationEntity;
import com.platform.entity.ProductEntity;
import com.platform.service.MerchantInfomationService;
import com.platform.service.ProductService;
import com.platform.utils.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.platform.entity.TOrderCommentEntity;
import com.platform.service.TOrderCommentService;

/**
 * 订单商品评论Controller
 *
 * @author liweihai
 * @email liweihai12345@sina.com
 * @date 2019-05-18 14:59:10
 */
@RestController
@RequestMapping("/tordercomment")
public class TOrderCommentController {
    @Autowired
    private TOrderCommentService tOrderCommentService;
    @Autowired
    private ProductService productService;
    @Autowired
    private MerchantInfomationService merchantInfomationService;
    /**
     * 查看列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<TOrderCommentEntity> tOrderCommentList = tOrderCommentService.queryList(query);
        int total = tOrderCommentService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(tOrderCommentList, total, query.getLimit(), query.getPage());
        for (TOrderCommentEntity orderCommentEntity : tOrderCommentList){
            String imgs = orderCommentEntity.getCommentImg();
            if(!StringUtils.isNullOrEmpty(imgs)){
                String[] img = null;
                if(imgs.indexOf(",")>-1){
                    img = imgs.split(",");
                    orderCommentEntity.setImgs(img);
                }else{
                    img = new String[]{imgs};
                    orderCommentEntity.setImgs(img);
                }

            }
        }

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        TOrderCommentEntity tOrderComment = tOrderCommentService.queryObject(id);
        String imgs = tOrderComment.getCommentImg();
        if(!StringUtils.isNullOrEmpty(imgs)){
            String[] img = null;
            if(imgs.indexOf(",")>-1){
                img = imgs.split(",");
                tOrderComment.setImgs(img);
            }else{
                img = new String[]{imgs};
                tOrderComment.setImgs(img);
            }

        }
        return R.ok().put("tOrderComment", tOrderComment);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody TOrderCommentEntity tOrderComment) {
        tOrderCommentService.save(tOrderComment);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody TOrderCommentEntity tOrderComment) {
        tOrderCommentService.update(tOrderComment);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        tOrderCommentService.updateBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<TOrderCommentEntity> list = tOrderCommentService.queryList(params);

        return R.ok().put("list", list);
    }

    @RequestMapping("/queryMerchantList")
    public R queryMerchantList(){
        List<MerchantInfomationEntity> merchantInfomationEntities = merchantInfomationService.queryAllMerchantList();
        return R.ok().put("list",merchantInfomationEntities);
    }

    @RequestMapping("queryPorductList")
    public R queryPorductList(@RequestParam("merchantId")Integer merchantId){
        List<ProductEntity> productEntities = productService.queryObjectByMId(merchantId);
        return R.ok().put("list",productEntities);
    }
}
